package fr.expdev.bndd;

import java.util.List;

import android.app.Dialog;
import android.app.ListActivity;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.ListView;

import fr.expdev.bndd.data.Post;
import fr.expdev.bndd.data.PostAdapter;
import fr.expdev.bndd.data.PostsAsyncTask;

public class Main extends ListActivity {
	
	private static final String TAG = "Main";

	private PostAdapter _adapter;

	private ImageView _logoImage;

	private AppContext appContext = AppContext.getAppContext();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		if (savedInstanceState == null) {
			appContext.posts.add(new Post("Loading...", null, "", Post.TYPE_LOADING));

			_adapter = new PostAdapter(this, appContext.posts);
			_logoImage = (ImageView) findViewById(R.id.logo);

			setListAdapter(_adapter);

			_logoImage.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Log.i(TAG, "Rafraichissement via le logo");
					refresh();
				}
			});

			refresh();
		}

	}

	@Override
	public void finish() {
		super.finish();
		System.exit(RESULT_OK);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.menu.rssmenu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case R.id.menu_refresh:
				refresh();
				return true;
				
			case R.id.menu_infos:
				final Dialog infosDialog = new Dialog(this);
				infosDialog.setTitle("Infos de l'application");
				infosDialog.setContentView(R.layout.infos_dialog);
				
				String versionName = "non-défini";
				try {
					versionName = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
				} catch (NameNotFoundException e) {
					e.printStackTrace();
				}
				final String content = String.format(getString(R.string.infos_changelog), versionName);
				
				((WebView)infosDialog.findViewById(R.id.infos_dialog_changelog)).loadDataWithBaseURL(
						"http://www.experience-developpement.fr/", 
						content, 
						"text/html", 
						"utf-8", 
						"about:blank"
				);

				infosDialog.show();
				return true;
		}
		return false;
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Intent intent = new Intent(Main.this, DetailPost.class);
		intent.putExtra("post_number", position);
		intent.addFlags(Intent.FLAG_FROM_BACKGROUND);
		startActivity(intent);
	}

	private void refresh() {
		_logoImage.setBackgroundResource(R.drawable.loading_animated);
		((AnimationDrawable) _logoImage.getBackground()).start();
		PostsAsyncTask task = new PostsAsyncTask(this);
		task.execute("http://www.experience-developpement.fr/feed/");
	}

	public void updatePosts(List<Post> posts) {
		if (posts.size() == 0) {
			Log.e(TAG, "Aucun article n'a été chargé");
		}
		appContext.posts = posts;
		_adapter.setPosts(posts);
		_adapter.notifyDataSetChanged();

		((AnimationDrawable) _logoImage.getBackground()).stop();
		_logoImage.setBackgroundResource(R.drawable.icon);
	}

}